import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/generated/locales.g.dart';

class ChoiceDialog extends Dialog {
  final String title;
  final List pickerLists;
  final String initialItem;
  final String unit;
  final Function callback;

  ChoiceDialog(
      {required this.title,
      required this.initialItem,
      required this.unit,
      required this.pickerLists,
      required this.callback});

  @override
  Widget build(BuildContext context) {
    int selectedIndex = pickerLists.indexOf(initialItem);
    if (selectedIndex <= 0) {
      selectedIndex = 0;
    }

    return GestureDetector(
        // 点击遮罩层隐藏弹框
        child: Material(
            type: MaterialType.transparency, // 配置透明度
            child: Center(
                child: GestureDetector(
                    // 点击遮罩层关闭弹框，并且点击非遮罩区域禁止关闭
                    onTap: () {
                      print('～');
                    },
                    child: Container(
                        width: 916.w,
                        height: 736.w,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(10),
                          color: Colors.white,
                        ),
                        child: Column(children: <Widget>[
                          Container(
                              alignment: Alignment.topCenter,
                              margin: const EdgeInsets.fromLTRB(0, 19, 0, 19),
                              child: Text(title,
                                  style: TextStyle(
                                    fontSize: 54.sp,
                                    color: const Color(0xff000000),
                                  ))),
                          Expanded(
                            child: Container(
                              margin: const EdgeInsets.fromLTRB(0, 0, 0, 19),
                              child: Stack(
                                children: [
                                  CupertinoPicker(
                                    diameterRatio: 2,
                                    useMagnifier: true,
                                    magnification: 1.1,
                                    squeeze: 1,
                                    itemExtent: 30,
                                    scrollController: FixedExtentScrollController(
                                      initialItem: selectedIndex,
                                    ),
                                    onSelectedItemChanged: (value) {
                                      selectedIndex = value;
                                    },
                                    children: pickerLists.map((e) {
                                      return Text(e);
                                    }).toList(),
                                  ),
                                  Container(
                                    alignment: Alignment.centerRight,
                                    padding: const EdgeInsets.only(right: 22),
                                    child: Text(
                                      unit,
                                      style: TextStyle(
                                        fontSize: 54.sp,
                                        color: Colors.black,
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                          Container(
                              height: 43,
                              child: Row(children: <Widget>[
                                Expanded(
                                    flex: 1,
                                    child: GestureDetector(
                                        behavior: HitTestBehavior.opaque,
                                        child: Container(
                                            height: double.infinity,
                                            alignment: Alignment.center,
                                            decoration: const BoxDecoration(border: Border(right: BorderSide(width: 1, color: Color(0xffEFEFF4)))),
                                            child: Text(LocaleKeys.person_choice_cancel.ts,
                                                textAlign: TextAlign.center,
                                                style: TextStyle(color: Colors.black, fontSize: 54.sp, fontWeight: FontWeight.w400))),
                                        onTap: () {
                                          Navigator.pop(context);
                                        })),
                                Expanded(
                                    flex: 1,
                                    child: GestureDetector(
                                        behavior: HitTestBehavior.opaque,
                                        child: Container(
                                            height: double.infinity,
                                            // 继承父级的高度
                                            alignment: Alignment.center,
                                            child: Text(LocaleKeys.person_choice_submit.ts,
                                                textAlign: TextAlign.center,
                                                style: TextStyle(
                                                  color: Colors.black,
                                                  fontSize: 54.sp,
                                                ))),
                                        onTap: () {
                                          callback(selectedIndex, pickerLists[selectedIndex]);
                                          Navigator.pop(context); // 关闭dialog
                                        }))
                              ]))
                        ]))))),
        onTap: () {
          Navigator.pop(context);
        });
  }
}
